package com.jiu.bean;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import net.sourceforge.jpcap.capture.PacketCapture;
import net.sourceforge.jpcap.capture.PacketListener;
import net.sourceforge.jpcap.net.Packet;
import net.sourceforge.jpcap.net.TCPPacket;

import com.jiu.core.Account;
import com.jiu.core.FishContext;

public class MyJPcap extends Thread{
	private FishContext context;
	public MyJPcap(FishContext context) {
		this.context = context;
		
	}
	@Override
	public void run() {
		PacketCapture pcap = new PacketCapture();
		String device;
		try {
			device = pcap.findDevice();//找网卡
			device = device.substring(0, device.lastIndexOf('}') + 1);//处理并得到网卡地址
			pcap.open(device, true);//打开网卡资源
			Long currDay = System.currentTimeMillis()/86400000*7;
			JOptionPane.showMessageDialog(context.getMainFrame(),
											"今天的退出口令为："+Long.toHexString(currDay)+"(每天更新)"
											+ "\nOpen device :" + device + " successed!");
			System.err.println("open device :" + device + " successed!");
			pcap.addPacketListener(new TcpMsgPicker(context,pcap));
			pcap.capture(-1);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(context.getMainFrame(), "未找到网卡");
		}
	}

}
class TcpMsgPicker implements PacketListener {
	@SuppressWarnings("unused")
	private PacketCapture pcap;
	private FishContext context;
	private String HOST;
	private String isoData;
	TcpMsgPicker(FishContext context, PacketCapture pcap){
		this.pcap=pcap;
		this.context = context;
		this.HOST = context.getConfig().getProp().getProperty("service.ipaddr");
	}
	public void packetArrived(Packet packet) {
		
		try {
			if (packet instanceof TCPPacket && packet.getData().length >= 50) {
				TCPPacket tcpPacket = (TCPPacket) packet;
				String srcHost = tcpPacket.getSourceAddress();
				String dstHost = tcpPacket.getDestinationAddress();
				byte[] data = tcpPacket.getTCPData();
				Account account = context.getAccount();
				String temp = new String(data, "utf-8");
				if(temp.contains("</soap:Envelope>")&&!temp.contains("<?xml"))
					isoData += temp;
				else
					isoData = temp;
				if (srcHost.equals(HOST)) {
					//接收包
				} else if (dstHost.equals(HOST)&&isoData.contains(account.getQq())) {// 发送包
					if(account.getVerifyCode()==null){
						catchPacketInfo(isoData);
						//pcap.close();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void catchPacketInfo(String isoData) {
		Pattern	pattern = Pattern.compile("<Password>(\\w*)</Password><VerifyCode>(\\w*)</VerifyCode>");
		Matcher	matcher = pattern.matcher(isoData);
		if(matcher.find()){
			context.getAccount().setPassword(matcher.group(1));
			context.getAccount().setVerifyCode(matcher.group(2));
			JOptionPane.showMessageDialog(context.getMainFrame(),"软件已成功启动！");
		}
		pattern = Pattern.compile("\\d{1,2}");
		matcher = pattern.matcher(context.getAccount().getNickname());
		if(matcher.find()){
			context.getAccount().setGroupid(matcher.group());
			context.updateStatus(false);
			context.getSendMap().put(1, "#D"+new Random().nextInt(10));
			context.getSendThread().start();
			context.getFishThread().start();
		}
	}
}